<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="订单量" prop="orderQuantity">
          <el-input v-model="queryParams.orderQuantity" placeholder="请输入订单量" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="材质" prop="materialQuality">
          <el-input v-model="queryParams.materialQuality" placeholder="请输入材质" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="下单人" prop="orderingPerson">
          <!-- <el-input v-model="queryParams.orderingPerson" placeholder="请输入下单人" clearable @keyup.enter="handleQuery" /> -->
		  <el-select v-model="queryParams.orderingPerson" placeholder="请选择负责人" clearable @change="handleQuery">
                        <el-option v-for="(dict,i) in rw_user_list" :key="i" :label="dict.label" :value="dict.value" />
                    </el-select>
        </el-form-item>
        <el-form-item label="下单时间" prop="orderingTime">
          <el-date-picker clearable v-model="queryParams.orderingTime" type="date" value-format="YYYY-MM-DD"
            placeholder="选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货时间" prop="deliveryTime">
          <el-date-picker clearable v-model="queryParams.deliveryTime" type="date" value-format="YYYY-MM-DD"
            placeholder="选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="裁切人" prop="trimmer">
          <el-input v-model="queryParams.trimmer" placeholder="请输入裁切人" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="单片价格" prop="singlePiecePrice">
          <el-input v-model="queryParams.singlePiecePrice" placeholder="请输入单片价格" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus" >
            <el-select v-model="queryParams.orderStatus" placeholder="请选择状态" clearable>
                        <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label"
                            :value="dict.value" />
            </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['manage:form:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
            v-hasPermi="['manage:form:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['manage:form:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="Download" @click="handleExport"
            v-hasPermi="['manage:form:export']">导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="formList" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="编号" align="center" type="index" width="60" />
            <el-table-column label="订单量" align="center" prop="orderQuantity">
            </el-table-column>
            <el-table-column label="材质" align="center" prop="materialQuality">
            </el-table-column>
            <el-table-column label="颜色" align="center" prop="colour">
            </el-table-column>
            <el-table-column label="克重" align="center" prop="gramWeight">
            </el-table-column>
            <el-table-column label="尺寸" align="center" prop="size">
            </el-table-column>
            <el-table-column label="印刷方式" align="center" prop="printingMethod">
            </el-table-column>
            <el-table-column label="是否锁边" align="center" prop="isLock">
            </el-table-column>
            <el-table-column label="包装" align="center" prop="packing">
            </el-table-column>
            <el-table-column label="下单人" align="center" prop="orderingPerson">
            </el-table-column>
            <el-table-column label="下单时间" align="center" prop="orderingTime" width="180">
            </el-table-column>
            <el-table-column label="发货时间" align="center" prop="deliveryTime" width="180">
            </el-table-column>
            <el-table-column label="裁切人" align="center" prop="trimmer">
            </el-table-column>
            <el-table-column label="单片价格" align="center" prop="singlePiecePrice">
            </el-table-column>
            <el-table-column label="总价" align="center" prop="totalPrice">
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remarks">
            </el-table-column>
            <el-table-column label="订单状态" align="center" prop="orderStatus">
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-link icon="Edit" class="table_link_btn" type="primary" :underline="false" @click="handleUpdate(scope.row)"
                    v-hasPermi="['manage:content:edit']"><span class="table_link_text">修改</span></el-link>
                    <el-link icon="Delete" class="table_link_btn" type="danger" :underline="false" @click="handleDelete(scope.row)"
                    v-hasPermi="['manage:content:edit']"><span class="table_link_text">删除</span></el-link>
                </template>
            </el-table-column>
      </el-table>
  
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
        @pagination="getList" />
  
      <!-- 添加或修改订单对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="订单量" prop="orderQuantity">
            <el-input v-model="form.orderQuantity" placeholder="请输入订单量" />
          </el-form-item>
          <el-form-item label="材质" prop="materialQuality">
            <el-input v-model="form.materialQuality" placeholder="请输入材质" />
          </el-form-item>
          <el-form-item label="颜色" prop="colour">
            <el-input v-model="form.colour" placeholder="请输入颜色" />
          </el-form-item>
          <el-form-item label="克重" prop="gramWeight">
            <el-input v-model="form.gramWeight" placeholder="请输入克重" />
          </el-form-item>
          <el-form-item label="尺寸" prop="size">
            <el-input v-model="form.size" placeholder="请输入尺寸" />
          </el-form-item>
          <el-form-item label="印刷方式" prop="printingMethod">
            <el-input v-model="form.printingMethod" placeholder="请输入印刷方式" />
          </el-form-item>
          <el-form-item label="是否锁边" prop="isLock">
            <el-input v-model="form.isLock" placeholder="请输入是否锁边" />
          </el-form-item>
          <el-form-item label="包装" prop="packing">
            <el-input v-model="form.packing" placeholder="请输入包装" />
          </el-form-item>
          <el-form-item label="客户" prop="orderingPerson">
            <!-- <el-input v-model="form.orderingPerson" placeholder="请输入下单人" /> -->
			<el-select v-model="form.orderingPerson" placeholder="请选择客户" clearable @change="handleQuery">
                        <el-option v-for="(dict,i) in rw_user_list" :key="i" :label="dict.label" :value="dict.value" />
                    </el-select>
          </el-form-item>
          <el-form-item label="下单时间" prop="orderingTime">
            <el-date-picker clearable v-model="form.orderingTime" type="date" value-format="YYYY-MM-DD"
              placeholder="选择下单时间">
            </el-date-picker>
         </el-form-item>
          <el-form-item label="发货时间" prop="deliveryTime">
            <el-date-picker clearable v-model="form.deliveryTime" type="date" value-format="YYYY-MM-DD"
              placeholder="选择发货时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="裁切人" prop="trimmer">
            <el-input v-model="form.trimmer" placeholder="请输入裁切人" />
          </el-form-item>
          <el-form-item label="单片价格" prop="singlePiecePrice">
            <el-input v-model="form.singlePiecePrice" placeholder="请输入单片价格" />
          </el-form-item>
          <el-form-item label="总价" prop="totalPrice">
            <el-input v-model="form.totalPrice" placeholder="请输入总价" />
          </el-form-item>
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" placeholder="请输入备注" />
          </el-form-item>
          <el-form-item label="订单状态">
            <el-radio-group v-model="form.status">
              <el-radio v-for=" dict in sys_normal_disable " :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitOrder">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
<script setup name="Order" lang="ts">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from './api/order';
import { ref } from 'vue';
import { getCurrentInstance} from 'vue';
const { proxy } = getCurrentInstance() as any;
const { sys_normal_disable,rw_user_list } = proxy.useDict('sys_normal_disable','rw_user_list');

const formList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');
  
const form = ref<any>({});
const queryParams = ref({
	pageNum: 1,
	pageSize: 10,
	orderQuantity: '', materialQuality: '', colour: null, gramWeight: null, size: null, printingMethod: null, isLock: '',packing: null, orderingPerson:'',orderingTime:'',deliveryTime:'',trimmer:'',singlePiecePrice:'',totalPrice:'',remarks:'', orderStatus:'',
});
const rules = ref({});
  

/** 查询订单列表 */
function getList() {
	loading.value = true;
	listOrder(queryParams.value).then((response:any) => {
		formList.value = response.data.list;
		total.value = response.data.total;
		loading.value = false;      
	});
}
  
// 取消按钮
function cancel() {
	open.value = false;
	reset();
}
  
// 表单重置
function reset() {
	form.value = {
		id: null, orderQuantity: null, materialQuality: null, colour: null, gramWeight: null, size: null, printingMethod: null, isLock: null, packing: null, orderingPerson: null, orderingTime: null, deliveryTime: null, trimmer: null, singlePiecePrice: null, totalPrice: null, remarks: null, orderStatus: '0'
	};
	proxy.resetForm('formRef');
}
  
/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
}
  
/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm('queryRef');
	handleQuery();
}
  
// 多选框选中数据
function handleSelectionChange(selection:any) {
	ids.value = selection.map((item:any) => item.id);
	single.value = selection.length !== 1;
	multiple.value = !selection.length;
}
  
/** 新增按钮操作 */
function handleAdd() {
	reset();
	open.value = true;
	title.value = '添加订单';
}
  
/** 修改按钮操作 */
function handleUpdate(row:any) {
	reset();
	const id = row.id || ids.value;
	getOrder(id).then((response:any) => {
		form.value = response.data;
		open.value = true;
		title.value = '修改订单';
	});
}
  
/** 提交按钮 */
function submitOrder() {
	proxy.$refs['formRef'].validate((valid:any) => {
		if (valid) {
			if (form.value.id != null) {
				updateOrder(form.value).then((response:any) => {
					proxy.$modal.msgSuccess('修改成功');
					open.value = false;
					getList();
				});
			} else {
				addOrder(form.value).then((response:any) => {
					proxy.$modal.msgSuccess('新增成功');
					open.value = false;
					getList();
				});
			}
		}
	});
}
  
/** 删除按钮操作 */
function handleDelete(row:any) {
	const res= row.id || ids.value;
	proxy.$modal.confirm('是否确认删除订单编号为"' + res + '"的数据项？').then(function () {
		return delOrder(res);
	}).then(() => {
		getList();
		proxy.$modal.msgSuccess('删除成功');
	}).catch(() => { });
}
  
/** 导出按钮操作 */
function handleExport() {
	proxy.download('manage/form/export', {
		...queryParams.value
	}, `form_${new Date().getTime()}.xlsx`);
}
  
getList();


</script>
  